import Vue from 'vue'
import Router, { RouteConfig } from 'vue-router'
import Home from '@views/home/index.vue'
/* Layout */
import Layout from '@layouts/index.vue'
import error401 from '@components/error-page/401.vue'
import error404 from '@components/error-page/404.vue'
Vue.use(Router)

export const constantRoutes: RouteConfig[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () =>
          import(
            /* webpackChunkName: "redirect" */ '@/views/redirect/index.vue'
          )
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home,
        name: 'Home',
        meta: { title: 'Home', icon: 'dashboard', affix: true }
      },
      {
        path: '/inspire',
        component: () => import('@views/About.vue'),
        name: 'About',
        meta: { title: 'About', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    path: '/login',
    component: Layout,
    redirect: '/login/index',
    children: [
      {
        path: 'index',
        component: () => import('@views/login/index.vue'),
        name: 'Login',
        meta: {
          title: 'Login',
          icon: 'user',
          affix: true
        }
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@views/About.vue')
  },
  {
    path: '/404',
    name: '404',
    component: error404
  },
  {
    path: '/401',
    name: '401',
    component: error401
  },
  {
    path: '*',
    redirect: '/404',
    meta: { hidden: true }
  }
]

const createRouter = () =>
  new Router({
    mode: 'history', // Disabled due to Github Pages doesn't support this, enable this if you need.
    scrollBehavior: (to, from, savedPosition) => {
      if (savedPosition) {
        return savedPosition
      } else {
        return { x: 0, y: 0 }
      }
    },
    base: process.env.BASE_URL,
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  ;(router as any).matcher = (newRouter as any).matcher // reset router
}

export default router
